package m2;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;

public class Producer {
    private static String[] msg={
            "1,h",
            "2,I",
            "6,P",
            "8,Z",
            "7,F"
    };;



    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer p= new DefaultMQProducer("producer-group-2");

        p.setNamesrvAddr("192.168.64.141:9876");

        p.start();

        for (String s: msg){
            String[] pa=s.split(",");
            Long orderId=Long.valueOf(pa[0]);
            Message m=new Message("Topic2",s.getBytes());
            p.send(m,new MessageQueueSelector(){
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    Long orderId=(Long) o;
                    int index=(int) (orderId % list.size());
                    return  list.get(index);
                }
            },orderId);
            SendResult r=p.send(m);
            System.out.println(r);
        }
    }
}
